#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>

bool np[60007];
std::vector<int> prime;
void sieve(int n) {
  np[1] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!np[i]) {
      prime.push_back(i);
    }
    for (int j : prime) {
      if (i * j > n) {
        break;
      }
      np[i * j] = 1;
      if (i % j == 0) {
        break;
      }
    }
  }
}
bool good(int x) { return !np[x] && (x <= 10 || x % 20 == 3); }

int n, m, o[8007], x[8007], y[8007];

int dis(int i, int j) { return std::abs(x[i] - x[j]) + std::abs(y[i] - y[j]); }

std::bitset<8007> l[8007], r[8007];
std::vector<std::pair<int, int>> s[60007];

void solve() {
  std::cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    std::cin >> x[i] >> y[i];
  }
  for (int i = 1; i <= n; ++i) {
    std::cin >> o[i];
    l[i].reset(), r[i].reset();
  }

  if (n <= 100) {
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j < i; ++j) {
        for (int k = 1; k < j; ++k) {
          std::vector<int> v{dis(i, j), dis(j, k), dis(k, i)};
          std::sort(v.begin(), v.end());
          if (good(v[1])) {
            ans += o[i] + o[j] + o[k];
          }
        }
      }
    }
    std::cout << ans << '\n';
    return;
  }

  for (int i = 1; i <= m + m; ++i) {
    std::vector<std::pair<int, int>>().swap(s[i]);
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < i; ++j) {
      int d = dis(i, j);
      s[d].push_back({i, j});
      if (d != 1) {
        r[i][j] = r[j][i] = 1;
      }
    }
  }

  long long ans1 = 0, ans2 = 0, ans3 = 0;
  for (int d = 1; d <= m + m - 2; ++d) {
    if (good(d)) {
      for (auto e1 : s[d]) {
        for (auto e2 : s[d]) {
          std::vector<int> v{e1.first, e1.second, e2.first, e2.second};
          std::sort(v.begin(), v.end()), v.erase(std::unique(v.begin(), v.end()), v.end());
          if (v.size() == 3u) {
            if (dis(v[0], v[1]) == d && dis(v[1], v[2]) == d && dis(v[2], v[0]) == d) {
              ans3 += o[v[0]] + o[v[1]] + o[v[2]];
            } else {
              ans2 += o[v[0]] + o[v[1]] + o[v[2]];
            }
          }
        }
      }
      for (auto [i, j] : s[d]) {
        ans1 += 3 * ((l[i] & r[j]).count());
        ans1 += 3 * ((l[j] & r[i]).count());
      }
    }
    for (auto e : s[d]) {
      l[e.first][e.second] = l[e.second][e.first] = 1;
    }
    for (auto e : s[d + 1]) {
      r[e.first][e.second] = r[e.second][e.first] = 0;
    }
  }

  std::cout << ans1 + ans2 / 2 + ans3 / 6 << '\n';
}

int main() {
  std::freopen("chess.in", "r", stdin);
  std::freopen("chess.out", "w", stdout);
  std::cin.tie(nullptr)->sync_with_stdio(false);
  sieve(60000);
  int t;
  std::cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}